package com.shuqi.activity;

import java.util.ArrayList;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.KeyEvent;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.ImageView;

import com.shuqi.activity.viewport.MyViewPager;
import com.shuqi.activity.viewport.MyViewPager.ViewListener;
import com.shuqi.controller.R;

/**
 * @author linyuren 新手引导界面
 */
public class NewbieGuideActivity extends ActivityBase implements OnClickListener {

    /** 记录是否从logo进入 */
    private boolean isAutoFinish;

    // 定义ViewPager对象
    private MyViewPager mViewPager;

    // 定义一个ArrayList来存放View
    private ArrayList<View> mViews;

    /** 页面背景色 */
    private final int[] IDS_BGCOLOR = {0xfff3e0ae, 0xfff1c6d1, 0xff83cb9d};
    /** 引导图资源ID */
    private final int[] IDS_IMAGE = {R.drawable.img_guider1, R.drawable.img_guider2, R.drawable.img_guider3};
    /** 引导图下方的位置图片资源ID */
    private final int[] IDS_POINT = {R.drawable.img_guider1_piont, R.drawable.img_guider2_piont, R.drawable.img_guider3_piont};


    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        this.setContentView(R.layout.act_newbie_guide_view);
        this.isAutoFinish = getIntent().getBooleanExtra("isAutoFinish", false);
        activityLogicForward();
        activityInitData();
    }

    @Override
    public void onClick(View v) {
        if (v.getId() == R.id.newbie_guide_button) {
            BookShelfActivity.showFromLoading(this, true);
            mFinishCurrent(false);
        }
    }

    @Override
    public void activityLogicForward() {
        // 实例化ArrayList对象
        mViews = new ArrayList<View>();
        // 实例化ViewPager
        mViewPager = (MyViewPager) findViewById(R.id.viewpager);
    }

    @Override
    public void activityInitData() {
        mViews.clear();
        for (int i = 0; i < IDS_IMAGE.length; i++) {
            View view = getLayoutInflater().inflate(R.layout.newbie_guide_item, null);
            view.findViewById(R.id.newbie_guide_bg).setBackgroundColor(IDS_BGCOLOR[i]);// 背景色
            ((ImageView) view.findViewById(R.id.newbie_guide_img)).setImageResource(IDS_IMAGE[i]);// 图片
            ((ImageView) view.findViewById(R.id.newbie_guide_position))
                    .setImageResource(IDS_POINT[i]);// 位置点
            if (i == IDS_IMAGE.length - 1) {
                if(isAutoFinish){
                    view.findViewById(R.id.newbie_guide_button).setVisibility(View.GONE);
                    mViewPager.setOnChangeListener(mListener, true);
                }else{
                    view.findViewById(R.id.newbie_guide_button).setVisibility(View.VISIBLE);
                    view.findViewById(R.id.newbie_guide_button).setOnClickListener(this);
                }
            }
            mViews.add(view);
        }
        // 设置数据
        mViewPager.setAdapter(mViews);
    }

    /**
     * 打开新手引导页：
     * @param activity
     * @param isAutoFinish  是否自动关闭引导页（不自动关闭时，需要点击“立即体验”关闭）
     */
    public static void show(Activity activity, boolean isAutoFinish) {
        Intent intent = new Intent(activity, NewbieGuideActivity.class);
        intent.putExtra("isAutoFinish", isAutoFinish);
        ActivityManager.getInstance().startNextActivity(intent, activity);
    }

    /** 滑动到最后一页监听事件 */
    private ViewListener mListener = new ViewListener() {
        @Override
        public void onEndPageSelected() {
            mFinishCurrent(false);
        }
        @Override
        public void onPageSelected(int arg0) {}
        @Override
        public void onPageScrolled(int arg0, float arg1, int arg2) {}
        @Override
        public void onPageScrollStateChanged(int arg0) {}
    };

    /** 结束当前界面 */
    private void mFinishCurrent(boolean showAnim) {
        MyViewPager.isCenter = false;
        if(showAnim){
            ActivityManager.getInstance().finishActivity(this);
        }else{
            finish();
        }
    }


    @Override
    public boolean onKeyDown(int keyCode, KeyEvent event) {
        if (keyCode == KeyEvent.KEYCODE_BACK) {
            if (isAutoFinish) {
                mFinishCurrent(true);
            } else {
                return true;
            }
        }
        return super.onKeyDown(keyCode, event);
    }
}
